package homework;

/**
 * @Author Stringzhua
 * @Date 2024/2/29 19:49
 * description:定义方法find,实现查找某字符串是否在z数组中,并返回索引
 * 若找不到,则返回-1
 */
public class homework02 {
    public static void main(String[] args) {
        String[] strs = {"jack", "tom", "marry", "milan"};
        A02 a02 = new A02();
        int index = a02.find("tom", strs);
        System.out.println("查找的index=" + index);
    }
}

class A02 {
    /*
     * 类名A02
     * 方法名 find
     * 返回值 int
     * 形参 (String,String[])
     * */
    public int find(String findStr, String[] strs) {
        //直接遍历字符串数组,如果找到,则返回索引
        for (int i = 0; i < strs.length; i++) {
            if (findStr.equals(strs[i])) {
                return i;
            }
        }
        return -1;
    }
}
